package com.xr.util;

import java.util.concurrent.*;

/**
 * @author Arnold Yand
 * @summary 线程池工具类
 */
public class ThreadPoolUtil {

  protected static int MAX_FIXED_THREAD_SIZE = 5;

  private ThreadPoolUtil() {
  }

  public static void submit(Runnable task) {
    getFixedThreadPoolInstance().submit(task);
  }

  public static Future submit(Callable task) {
    return getFixedThreadPoolInstance().submit(task);
  }

  private static ExecutorService getFixedThreadPoolInstance() {
    return LocalNewFixedThreadPool.newFixedThreadPool;
  }

  private static class LocalNewFixedThreadPool {
    // 创建一个定长线程池，可控制线程最大并发数，超出的线程会在队列中等待。
    private static ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(MAX_FIXED_THREAD_SIZE);
  }
}
